import tkinter as tk
from tkinter import ttk, filedialog,messagebox
from PIL import Image, ImageTk
from intelligence.face import FaceRecognition
from middleWare import Middleware
from dao.Result_feature import Result

class FaceEntryApp(tk.Frame):
    def __init__(self, root,title="录入人脸", window_size="300x400"):
        self.root = root
        self.root.title(title)
        self.root.iconbitmap("static/favicon.ico")
        self.root.geometry(f"{window_size}+{int((self.root.winfo_screenwidth() - 300) / 2)}+{int((self.root.winfo_screenheight() - 400) / 2)}")
        self.root.configure(bg="#ffffff")

        self.create_widgets()

    def browse_file(self):
        self.file_path = filedialog.askopenfilename()
        self.show_image(self.file_path)

    def show_image(self, file_path):
        try:
            image = Image.open(file_path)
            image = image.resize((100, 100))
            photo = ImageTk.PhotoImage(image)

            self.image_label.config(image=photo)
            self.image_label.image = photo
        except Exception as e:
            print(f"Error loading image: {e}")

    def submit(self):
        name = self.name_entry.get()
        en_name = self.en_name_entry.get()
        class_ = self.class_entry.get()
        mid = Middleware()
        res = Result(**mid.get_config())
        face = FaceRecognition()
        try:
            if self.file_path != " " and name != "" and  class_ != "" and en_name != "": 
                face.add_db(image_path=self.file_path,label=name,res=res,class_=class_,cn_name=en_name)
                messagebox.showinfo("成功","添加成功")
                # pass
            else:
                messagebox.showwarning("错误","请输入信息")
        except:
            messagebox.showwarning("错误","请选择图片")

        

    def create_widgets(self):
        img_label = tk.Label(self.root, text='人像', bg='#ffffff')
        img_label.place(x=30, y=80)

        browse_button = tk.Button(self.root, text="上传图片", command=self.browse_file)
        browse_button.config(bg='#3b80f9', fg='#ffffff', relief='flat')
        browse_button.place(x=70, y=80)

        self.image_label = tk.Label(self.root, bg="#ffffff")
        self.image_label.place(x=140, y=40)

        self.class_label = tk.Label(self.root,text="班级",bg="#ffffff")
        self.class_label.place(x=30,y=160)

        self.class_entry = tk.Entry(self.root,font=('Arial',12),bg="#fafafa")
        self.class_entry.place(x=70,y=160)
        
        en_name_label = tk.Label(self.root,text="姓名",bg="#ffffff")
        en_name_label.place(x=30,y=210)

        self.en_name_entry = tk.Entry(self.root,font=('Arial',12),bg='#fafafa')
        self.en_name_entry.place(x=70,y=210)

        name_label = tk.Label(self.root, text="英文", bg="#ffffff")
        name_label.place(x=30, y=260)

        self.name_entry = tk.Entry(self.root, textvariable="姓名", font=("Arial", 12), bg='#fafafa')
        self.name_entry.place(x=70, y=260)

        submit_button = tk.Button(self.root, text="提交", command=self.submit)
        submit_button.config(bg='#3b80f9', fg='#ffffff', relief='flat', width=20)
        submit_button.place(x=80, y=340)

    def run(self):
        self.root.mainloop()

# Instantiate the FaceEntryApp class
# root = tk.Tk()
# app = FaceEntryApp(root)
# app.run()
